package com.xiaomili.halfsugar.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.google.gson.Gson;
import com.xiaomili.halfsugar.R;
import com.xiaomili.halfsugar.adapter.SearchManifestGridView_Adapter;
import com.xiaomili.halfsugar.bean.ManifestBean;
import com.xiaomili.halfsugar.constant.UrlString;
import com.xiaomili.halfsugar.utils.OKHttpUtils;


import java.io.IOException;

/**
 * 搜索页面---清单碎片
 * A simple {@link Fragment} subclass.
 */
public class SearchManifestFragment extends Fragment {


    private GridView mGridView;

    public SearchManifestFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_search_manifest, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mGridView = (GridView) view.findViewById(R.id.search_manifest_gv);
        OKHttpUtils.newInstance(getContext()).obtainDataAsyncByGet(UrlString.MANIFEST_URL, new OKHttpUtils.MyCallback() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                ManifestBean manifestBean = new Gson().fromJson(result, ManifestBean.class);
                SearchManifestGridView_Adapter adapter = new SearchManifestGridView_Adapter(getContext(), manifestBean);
                adapter.notifyDataSetChanged();
                mGridView.setAdapter(adapter);
            }
        });
    }
}
